//
//  SEFeedBackView.m
//  kuaiyi
//
//  Created by 张洪 on 16/7/8.
//  Copyright © 2016年 Alphts. All rights reserved.
//

#import "SEFeedBackView.h"



@interface SEFeedBackView()<UITextViewDelegate>

@property (nonatomic, weak) UILabel *remainLabel;



@end

@implementation SEFeedBackView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self setupSetting];
    }
    return self;
}

#pragma mark -布局视图
-(void)setupSetting{
    
    //初始化textView
    UITextView *textView = [[UITextView alloc] init];
    
    [textView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    
    textView.delegate = self;
    
    self.textView = textView;
    
    [self addSubview:textView];
    
    [textView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(75);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
        make.height.offset(180);
    }];
    
    //初始化字数限制Label
    UILabel *remainLabel = [[UILabel alloc] init];
    
    self.remainLabel = remainLabel;
    
    remainLabel.text = @"您还可以输入的字数为:200";
    
    [self addSubview:remainLabel];
    
    [remainLabel makeConstraints:^(MASConstraintMaker *make) {
        
        if (kDevice_Is_iPhone5) {
            make.left.equalTo(self).offset(20);
            
        }else if (kDevice_Is_iPhone6){
            make.left.equalTo(self).offset(50);
            
        }else if (kDevice_Is_iPhone6Plus){
            make.left.equalTo(self).offset(70);}
        
        make.top.equalTo(textView.bottom).offset(10);
        make.height.offset(40);
    }];

    
    //初始化提交按钮
    UIButton *commitBtn = [[UIButton alloc] init];
    
    [commitBtn setTitle:@"提交反馈" forState:UIControlStateNormal];
    
    [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [commitBtn setBackgroundColor:[UIColor colorWithRed:58/255.0 green:196/255.0 blue:196/255.0 alpha:1.0]];
    
    [self addSubview:commitBtn];
    
    [commitBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-10);
        make.top.equalTo(textView.bottom).offset(10);
        make.width.offset(80);
        make.height.offset(40);
    }];
    
    //初始化欢迎Label
    UILabel *noticeLabel = [[UILabel alloc] init];
    
    noticeLabel.text = @"    欢迎联系我们";
    
    [noticeLabel setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    
    [self addSubview:noticeLabel];
    
    [noticeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(remainLabel.bottom).offset(10);
        make.left.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
        make.height.offset(40);
    }];
    
    //联系方式
    UIImageView *callImageView = [[UIImageView alloc] init];
    
    callImageView.image =[UIImage imageNamed:@"dianhua"];
    
    [self addSubview:callImageView];
    
    [callImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10);
        make.top.equalTo(noticeLabel.bottom).offset(20);
        make.width.height.offset(20);
    }];
    
    UILabel *phoneLabel = [[UILabel alloc] init];
    
    phoneLabel.text = @"官服电话:400-636-6113";
    
    [self addSubview:phoneLabel];
    
    [phoneLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(callImageView.right).offset(20);
        make.top.equalTo(noticeLabel.bottom).offset(20);
        make.height.offset(20);
    }];
    
    UIImageView *QQImageView = [[UIImageView alloc] init];
    
    QQImageView.image =[UIImage imageNamed:@"qq"];
    
    [self addSubview:QQImageView];
    
    [QQImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10);
        make.top.equalTo(phoneLabel.bottom).offset(20);
        make.width.height.offset(20);
    }];
    
    UILabel *QQLabel = [[UILabel alloc] init];
    
    QQLabel.text = @"用户交流群：QQ 464768569";
    
    [self addSubview:QQLabel];
    
    [QQLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(QQImageView.right).offset(20);
        make.top.equalTo(phoneLabel.bottom).offset(20);
        make.height.offset(20);
    }];
}

//实现监听输入字数改变label显示
- (void)textViewDidChange:(UITextView *)textView{
    
    if (textView.text.length > 200) {
        
        textView.editable = NO;
  
        if (_feedBackBlock) {
            
            _feedBackBlock();
     
        }
        return;
        
    }else{
        
        self.remainLabel.text = [NSString stringWithFormat:@"您还可以输入的字数为:%lu",200 - textView.text.length];
    }
    
}

//实现当字数超过200后可以删除以及继续编辑
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if (text.length>200) {
      
        return NO;
        
    }else if([text isEqualToString:@""]){
        
        return YES;
    }
    
    return YES;
    
}


@end
